#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
#include <sstream>
using namespace std;

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1e4 + 10;
int n, m;

struct Record {
    int id;
    string title;
    string author;
    string keywords;
    string publisher;
    string year;

    bool operator< (const Record& r) const{
        return id < r.id;
    }
}record[N];

unordered_map<string, vector<int>> keywords_hash, title_hash, author_hash, publisher_hash, year_hash;

void query(int op, string s) {
    switch (op)
    {
    case 1:
        if (title_hash.find(s) != title_hash.end()) 
            for (auto & id : title_hash[s]) cout << id << endl;
        else 
            puts("Not Found");
        break;
    case 2:
        if (author_hash.find(s) != author_hash.end()) 
            for (auto & id : author_hash[s]) cout << id << endl;
        else 
            puts("Not Found");
        break;
    case 3:
    if (keywords_hash.find(s) != keywords_hash.end()) 
        for (auto & id : keywords_hash[s]) cout << id << endl;
    else 
        puts("Not Found");
    break;
    case 4:
    if (publisher_hash.find(s) != publisher_hash.end()) 
        for (auto & id : publisher_hash[s]) cout << id << endl;
    else 
        puts("Not Found");
    break;
    case 5:
    if (year_hash.find(s) != year_hash.end()) 
        for (auto & id : year_hash[s]) cout << id << endl;
    else 
        puts("Not Found");
    break;
    default:
        break;
    }
}

inline void solution() {
    cin >> n;
    getchar();
    for (int i = 0; i < n; i ++ ) {
        string idx;
        getline(cin, idx);
        record[i].id = stoi(idx);
        getline(cin, record[i].title);
        getline(cin, record[i].author);
        getline(cin, record[i].keywords);
        getline(cin, record[i].publisher);
        getline(cin, record[i].year);
    }
    sort(record, record + n);

    for (int i = 0; i < n; i ++ ) {
        int idx = record[i].id;
        // if (title_hash.find(record[i].title) == title_hash.end()) 
        title_hash[record[i].title].push_back(idx);
        // if (author_hash.find(record[i].author) == author_hash.end()) 
        author_hash[record[i].author].push_back(idx);
        // if (publisher_hash.find(record[i].publisher) == publisher_hash.end()) 
        publisher_hash[record[i].publisher].push_back(idx);
        // if (year_hash.find(record[i].year) == year_hash.end()) 
        year_hash[record[i].year].push_back(idx);
        string keywords = record[i].keywords;
        stringstream sin(keywords);
        string s;
        while (sin >> s) 
            keywords_hash[s].push_back(idx);
    }

    cin >> m;
    getchar();
    while (m -- ) {
        string s;
        getline(cin, s);
        cout << s << endl;
        int op = stoi(s.substr(0, 1));
        query(op, s.substr(3));
    }
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}
